// const SMSClient = require('@alicloud/sms-sdk')
const accessKeyId = 'LTAI3fa66E4b1lDb'
const secretAccessKey = 'VJGPJLNScbhvFbEJV228NeDYPGb7y2'
var http = require('http')
var https = require('https')
var querystring = require("querystring")

module.exports = function(app) {
	return new Remote(app);
};

var Remote = function(app) {
	this.app = app;
	Remote.GameService = this.app.get("GameService")
	//验证码列表
	Remote.captchaList = {}
    Remote.dbService = this.app.get("dbService")
    if(Remote.dbService && Remote.dbService.db){
    	Remote.db = Remote.dbService.db
    }
};
var remote = Remote.prototype
var local = {}

//请求发送验证码
remote.sendCaptcha = function(phone,cb) {
	var tmpCode = Math.floor(Math.random() * 8999) + 1000
	var post_data = JSON.stringify({
		"account" : "N311162_N4146445",
		"password" : "zrgcWqty4l6e01",
		"msg" : "您的验证码为："+tmpCode+"，您正进行身份验证，温馨提示，如非本人操作，请忽略本短信！",
		"phone" : phone,
		'report':'false'
	})
	var post_options = {
	    host: 'smssh1.253.com',
	    port: '443',
	    path: '/msg/send/json',
	    method: 'POST',
	    headers: {
	      'Content-Type': 'application/json; charset=UTF-8'
	    }
	 };
	var post_req = https.request(post_options, function (response) {
		var responseText=[];
		var size = 0;
		response.on('data', function (data) {
		  responseText.push(data);
		  size+=data.length;
		});
		response.on('end', function () {
		  // Buffer 是node.js 自带的库，直接使用
		  responseText = Buffer.concat(responseText,size);
		  var data = JSON.parse(responseText)
		  console.log(data)
		  if(data.code == 0){
		  	Remote.captchaList[phone] = {"code" : tmpCode,"phone" : phone}
		  	cb(true,data)
		  }else{
		  	cb(false,data)
		  }
		});
	});
	post_req.write(post_data);
	post_req.end();
}

// //确认验证码绑定手机
// remote.bindPhone = function(uid,code,cardNo,realName,cb) {
// 	var self = this
// 	if(Remote.captchaList[uid] && Remote.captchaList[uid].code == code){
// 		local.checkIdCard(cardNo,realName,function(data) {
// 			//保存身份信息
// 			if(data.error_code == 0 && data.reason == "认证通过"){
// 				var identityInfo = {}
// 				identityInfo.phone = Remote.captchaList[uid].phone
// 				identityInfo.cardNo = cardNo
// 				identityInfo.realName = realName
// 				self.app.rpc.db.remote.setPlayerObject(null,uid,"identityInfo",identityInfo,function(){})
// 				delete Remote.captchaList[uid]
// 			}
// 			cb(true,data)
// 		})
// 	}else{
// 		cb(false,"验证码错误")
// 	}
// }
//身份认证
// local.checkIdCard = function(cardNo,realName,cb) {
// 	//身份认证appcode
// 	var appcode = "b71d582d27ac479f9fbc377c7b1bfdf6"
// 	var postData=querystring.stringify({  
// 	    "cardNo":cardNo,
// 	    "realName" : realName
// 	})
// 	var options={
// 	  hostname:'1.api.apistore.cn',
// 	  path:'/idcard2?'+postData,
// 	  method:'GET',
// 	  headers:{
// 	    "Content-Type":"application/json; charset=utf-8",
// 	    'Authorization':"APPCODE "+appcode,
// 	    "Content-Length" : 0
// 	  }
// 	}
//     var req=http.get(options,function(res){
//         var responseText=[];
//         var size = 0;
//         res.on("data",function(chunk) {
// 		  responseText.push(chunk);
// 		  size+=chunk.length;
//         })
//         res.on("end",function() {
//         	responseText = Buffer.concat(responseText,size);
//         	var data = JSON.parse(responseText)
// 			console.log("=========22222")
// 			console.log(data)
// 			cb(data)
//         })
//     })
//     req.on('error', function(e) {
//       console.log("error")
//       console.error(e);
//       cb(false)
//     })
// }

//注册手机号
remote.register = function(phone,password,code,referrer,cb) {
	var self = this
	if(Remote.captchaList[phone] && Remote.captchaList[phone].code == code){
		delete Remote.captchaList[phone]
		Remote.dbService.db.hget("game:phoneAccount",phone,function(err,data) {
			if(data){
				console.log(err,data)
				cb(false,"账号已存在")
				return
			}
			Remote.dbService.db.hset("game:phoneAccount",phone,password)
			var result = {
				nickname : "unknown&%",
				phone : phone,
				referrer : referrer,
				headimgurl : 0,
				unionid : phone,
				openid : phone
			}
			self.app.rpc.db.remote.createAccount(null,result,function(flag) {
				cb(flag)
			})
		})
	}else{
		cb(false,"验证码错误")
	}
}

//重置密码
remote.changePassword = function(phone,password,code,cb) {
	if(Remote.captchaList[phone] && Remote.captchaList[phone].code == code){
		delete Remote.captchaList[phone]
		this.app.rpc.db.remote.changePassword(null,phone,password,function(flag,data) {
			cb(flag,data)
		})
	}else{
		cb(false,"验证码错误")
	}
}